var express = require('express');
var router = express.Router();

var tools = require('../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var middleware = require(tools.rootPath + 'wechat-pay').middleware;

//https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#58
//https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=1_1
//https://blog.csdn.net/angjbyszsh/article/details/81111391
//https://blog.csdn.net/m0_37422289/article/details/83069077
//car part
/*
car {
  openid: 'oC5lY1Z6uukn0OGyAhTzCTz0CklQ',
  trade_type: 'JSAPI',
  cash_fee_type: 'CNY',
  nonce_str: '0Cbm8TfAwh0kV9Eo1zvnsXxmOaPabpbU',
  time_end: '20201201141336',
  err_code_des: 'SUCCESS',
  return_code: 'SUCCESS',
  mch_id: '',
  settlement_total_fee: '101',
  sign: '7D53241FE8850A73CAA105A6AF52783E',
  cash_fee: '101',
  is_subscribe: 'Y',
  return_msg: 'OK',
  fee_type: 'CNY',
  bank_type: 'CMC',
  attach: '{"部位":"三角"}',
  device_info: 'sandbox',
  out_trade_no: 'kfc1606803215157',
  result_code: 'SUCCESS',
  total_fee: '101',
  appid: '',
  transaction_id: '4219241915420201201141336755417',
  err_code: 'SUCCESS'
}
*/

router.post('/car', middleware(config.wechatPay).getNotify().done(function (message, req, res, next) {
  console.log('car', message);

  var openid = message.openid;
  var order_id = message.out_trade_no;
  var attach = {};
  try {
    attach = JSON.parse(message.attach);
  } catch (e) { }

  /**
   * 查询订单，在自己系统里把订单标为已处理
   * 如果订单之前已经处理过了直接返回成功
   */
  res.reply('success');

  /**
   * 有错误返回错误，不然微信会在一段时间里以一定频次请求你
   * res.reply(new Error('...'))
   */
}));

//house part
/*
{
  openid: 'oC5lY1Z6uukn0OGyAhTzCTz0CklQ',
  trade_type: 'JSAPI',
  coupon_fee: '3',
  cash_fee_type: 'CNY',
  nonce_str: 'E7zbfCD4Kp9iEkn7cunAoLRrxno0qGEu',
  time_end: '20201201141802',
  err_code_des: 'SUCCESS',
  return_code: 'SUCCESS',
  mch_id: '',
  settlement_total_fee: '100',
  sign: 'C3AE4C2B06013B0DA092EE669B50FE50',
  cash_fee: '99',
  coupon_id_1: '67890',
  coupon_type_0: 'CASH',
  coupon_id_0: '123456',
  coupon_fee_0: '1',
  coupon_fee_1: '2',
  is_subscribe: 'Y',
  return_msg: 'OK',
  fee_type: 'CNY',
  bank_type: 'CMC',
  attach: '{"部位":"四角"}',
  device_info: 'sandbox',
  out_trade_no: 'kfc1606803481546',
  coupon_type_1: 'NO_CASH',
  result_code: 'SUCCESS',
  total_fee: '102',
  appid: '',
  coupon_count: '2',
  transaction_id: '4698524456520201201141802328157',
  err_code: 'SUCCESS'
}
*/
router.post('/house', middleware(config.wechatPay).getNotify().done(function (message, req, res, next) {
  console.log('house', message);

  var openid = message.openid;
  var order_id = message.out_trade_no;
  var attach = {};
  try {
    attach = JSON.parse(message.attach);
  } catch (e) { }

  /**
   * 查询订单，在自己系统里把订单标为已处理
   * 如果订单之前已经处理过了直接返回成功
   */
  res.reply('success');

  /**
   * 有错误返回错误，不然微信会在一段时间里以一定频次请求你
   * res.reply(new Error('...'))
   */
}));

module.exports = router;